package sz.szsmk.citizencard.ui.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.chenenyu.router.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.TransLogBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryCardAccInfoRes;
import sz.szsmk.citizencard.model.network.bean.response.TransLogRes;
import sz.szsmk.citizencard.mvp.contract.ITransLogContact;
import sz.szsmk.citizencard.mvp.presenter.TransLogPresenter;
import sz.szsmk.citizencard.ui.adapter.TransLogAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/18 20:40
 * description:
 */
@Route("trans")
public class TransLogActivity extends IBaseActivity<TransLogPresenter, TransLogBinding>
        implements ITransLogContact.ITransLogView, View.OnClickListener  {

    Spinner spinnerSimple;
    //    private SwipeRefreshLayout swiperefresh;
    private TransLogAdapter logAdapter;
    private String cardNo;
    private int currentPager = 1;
    private String type="0";
    private List<TransLogRes.TradeDetailsBean> translogList = new ArrayList<>();

    private ArrayList<String> mList = new ArrayList<>();
    private List<QueryCardAccInfoRes.AcctInforBean> acctInfor;

    @Override
    protected int attachLayoutRes() {
        return R.layout.trans_log;
    }

    @Override
    protected void initInjector() {

        DaggerCommonActivityComponent.builder()
                .appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        spinnerSimple = findViewById(R.id.spinner_simple);

        dataBinding.titleLayout.settingTitle.setText("交易记录");
        dataBinding.titleLayout.back.setOnClickListener(this);
        cardNo = getIntent().getStringExtra("cardNo");
        dataBinding.transLogRv.setVisibility(View.INVISIBLE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        dataBinding.transLogRv.setLayoutManager(linearLayoutManager);

        logAdapter = new TransLogAdapter(this);
//        dataBinding.transLogRv.startLoadFirst();
        dataBinding.transLogRv.setAdapter(logAdapter);
        mPresenter.queryCardAcctInfo(cardNo);

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPager = 1;
                translogList.clear();
                mPresenter.getTransLog(cardNo, type, currentPager);
            }
        });
        dataBinding.swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getTransLog(cardNo, type, currentPager);
            }
        });

    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void getTransLogSucc(TransLogRes response) {
//        swiperefresh.setRefreshing(false);
        List<TransLogRes.TradeDetailsBean> details = response.getTradeDetails();
        if (currentPager == 1) {
            this.translogList.clear();
        }
        translogList.addAll(details);

        if (details != null) {
            logAdapter.setItemData(translogList);
            logAdapter.notifyDataSetChanged();
        }

        dataBinding.transLogRv.setVisibility(View.VISIBLE);

        boolean hasMore = details.size() == 10;
        if (hasMore){
            currentPager++;
        }else{
            //禁止加载更多
            dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据
        }

    }

    @Override
    public void getTransLogFaie(String message) {
//        swiperefresh.setRefreshing(false);
        dataBinding.transLogRv.setVisibility(View.VISIBLE);
    }

    @Override
    public void queryCardAcctInfoFail() {

    }

    @Override
    public void queryCardAcctInfoSucc(final QueryCardAccInfoRes response) {
        acctInfor = response.getAcctInfor();
        mList.clear();
        mList.add("全部类型");
        for (int i = 0; i < acctInfor.size(); i++) {
            mList.add(acctInfor.get(i).getTypeName());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                TransLogActivity.this, android.R.layout.simple_spinner_item,
                mList);
        adapter.setDropDownViewResource(R.layout.spinner_item);
        spinnerSimple.setAdapter(adapter);
        spinnerSimple.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                translogList.clear();
                currentPager = 1;
                if (position == 0) {
                    type="0";
                    mPresenter.getTransLog(cardNo, "0", currentPager);
                } else {
                    type= response.getAcctInfor().get(position - 1).getAcctTypeNo();
                    mPresenter.getTransLog(cardNo, response.getAcctInfor().get(position - 1).getAcctTypeNo(), currentPager);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }

}
